
package dao;

import entities.Status;
import enums.StatusEnum;
import hibernateUtil.HibernateSessionFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;


import org.hibernate.Transaction;
import org.hibernate.classic.Session;

public class StatusDao {
    private static final Log log = LogFactory.getLog(StatusDao.class);
    
    public Status getByCode(StatusEnum statusEnum){
        Session session = null;
        Transaction transaction = null;
        log.debug("getting Status instance with code: " + statusEnum.name());
        try{
            session = HibernateSessionFactory.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            Query query = session.createQuery("from Status where code = :code");
            query.setParameter("code", statusEnum.name()); 
            return (Status) query.list().get(0);
        } catch (RuntimeException re){
            	log.error("get failed", re);
		throw re;
        } finally {
            transaction.commit();
            session.close();
        }
    }        
    
}
